<template>
  <div class="mainos">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>查看退货单</span>
          </div>
          <div>
            <el-button v-repeatClick @click="exportOrderiInfo">导出</el-button>
            <el-button v-repeatClick @click="print">打印</el-button>
          </div>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l marginLeft0"><em></em>单据信息</div>
        </div>
        <div class="main_xq">
          <ul class="boxha">
            <li>
              退货单号：<span>{{ info.order_sn }}</span>
            </li>
            <li>
              采购方式：<span>{{ base.modeList[info.mode] }}</span>
            </li>
            <li>
              采购员/供应商：<span>{{
                info.mode == 1
                  ? base.buyer[info.buyer_id]
                  : base.supplier[info.supplier_id]
              }}</span>
            </li>
            <li>
              退货金额：<span>{{ info.money }}</span>
            </li>
            <li>
              单据状态：<span>{{ base.status[info.status] }}</span>
            </li>
            <li>
              创建时间：<span>{{
                parseTime(info.created_at, "{y}-{m}-{d}")
              }}</span>
            </li>
            <li v-if="info.status == 1 ? true : false">
              退货时间：<span>{{
                parseTime(info.return_time, "{y}-{m}-{d}")
              }}</span>
            </li>
          </ul>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l marginLeft0"><em></em>商品详情</div>
        </div>
        <div class="main_table marginTop0">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="info.purchase_return_list"
            element-loading-text="加载中"
            fit
            highlight-current-row
            :summary-method="getSummaries"
            show-summary
          >
            <el-table-column label="序号" prop="userId">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
            </el-table-column>
            <el-table-column
              label="商品名称"
              width="150"
              prop="name"
              class-name="main_hei"
            >
              <template slot-scope="scope"
                >{{ scope.row.goods.name }}【{{
                  scope.row.goods_spu.spu_name.title
                }}】</template
              >
            </el-table-column>
            <el-table-column
              label="单位"
              prop="goods_spu.spu_unit.title"
              class-name="main_hei"
            />
            <el-table-column
              label="退货数量"
              width="150"
              prop="actuality_num"
              class-name="main_hei"
            />
            <el-table-column
              label="退货单价"
              width="150"
              prop="price"
              class-name="main_hei"
            />
            <el-table-column
              label="退货金额"
              width="150"
              prop="money"
              class-name="main_hei"
            >
            </el-table-column>
            <el-table-column label="备注" prop="remark" class-name="main_hei" />
          </el-table>
        </div>

        <!-- 生命周期 -->
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l marginLeft0"><em></em>生命周期</div>
        </div>
        <div class="main_table marginTop0" style="min-height: 230px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="logs"
            element-loading-text="加载中"
            fit
            highlight-current-row
          >
            <el-table-column
              label="管理员"
              prop="admin.name"
              class-name="main_hei"
            />
            <el-table-column
              label="操作"
              prop="operate.name"
              class-name="main_hei"
            />
            <el-table-column label="内容" prop="content" class-name="main_hei">
              <template slot-scope="scope">
                <div
                  v-for="(content, index) in scope.row.content"
                  :key="index"
                  class="marginTop5 marginBottom5"
                >
                  {{ content }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="IP" prop="ip" class-name="main_hei" />
            <el-table-column
              label="时间"
              prop="created_at"
              class-name="main_hei"
            >
              <template slot-scope="scope">{{
                parseTime(scope.row.created_at)
              }}</template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <!-- 打印子组件 -->
    <order-print
      ref="printTemplate"
      v-show="false"
      id="printname"
    ></order-print>
  </div>
</template>

<script>
import {
  purchaseReturn_base,
  purchaseReturn_info,
  export_purchasing_return,
} from "@/api/purchasing/purchasingReturn";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime,exportMethod } from "@/utils/index";
import OrderPrint from "@/views/order/orderPrint.vue";
export default {
  components: {
    OrderPrint,
  },
  computed: {
    total() {
      let sz = 0;
      this.info.purchase_return_list.forEach((e) => {
        sz += parseFloat(e.money);
      });
      return this.$toFixed(sz);
    },
  },
  data() {
    return {
      // 时间
      parseTime,
      exportMethod,
      // 基本信息
      info: {
        created_at: "2020-10-20",
        type: 0,
        status: 0,
        mode: 0,
        purchase_return_list: [],
      },
      logs: [],
      base: {
        modeList: ["后台"],
        status: ["后台"],
        buyer: ["后台"],
        supplier: ["后台"],
        typeList: ["待审核"],
      },
      listLoading: false,
      // id
      id: this.$route.params.id,
      // 是否编辑
      isEdit: false,
    };
  },
  created() {
    // 基本信息
    this.purchaseReturn_info();
    this.purchaseReturn_base();
  },
  methods: {
    // 基本信息
    purchaseReturn_info() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      purchaseReturn_info(data).then((res) => {
        if (res.errcode == 0) {
          res.data.logs.map((e) => {
            if (e.content) {
              e.content = JSON.parse(e.content);
            }
          });
          this.logs = res.data.logs;
          this.info = res.data.info;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    purchaseReturn_base() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      purchaseReturn_base(data).then((res) => {
        if (res.errcode == 0) {
          this.base = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 订单导出
    exportOrderiInfo() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
            const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_purchasing_return(data)
        .then((res) => {
          this.exportMethod(res, "采购退货", notify);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 打印
    print() {
      let id = this.id;
      this.$refs.printTemplate.$emit("demo", {
        id: id,
        type: "purchase_return",
      });
    },

    // 合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总价";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        }
      });
      sums[5] = this.info.money;
      sums.forEach((item, index) => {
        if (index != 0 && index != 5) {
          sums[index] = " ";
        }
      });
      return sums;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainos {
  .main_tile {
    .mainos_tile_r {
      color: $colour_red;
      font-size: $font_14;
    }
  }
}
</style>
